﻿#region Librerias

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ESF.General.MVP;
using ESF.SSO.Core.BusinessLogic;
using ESF.General.Entity;
using ESF.SSO.Core.Entities.BasicReferences;

#endregion


namespace ESF.SSO.Presenter.Admin.Menu
{
	public class EditUserGroupMenuPresenter : BasePresenter<IEditUserGroupMenuViewer>
	{
		#region Constructor

		public EditUserGroupMenuPresenter (IEditUserGroupMenuViewer pViewer) : base (pViewer) {}

		#endregion


		#region Funciones

		public override void Clear()
		{
			_Viewer.ApplicationID = null;
			_Viewer.UserGroupID = null;
			_Viewer.lstMainUserMenus = null;
		}

		
		public override void Load()
		{
			Clear ();
		}
		

		public List<UserMenuItem> GetUserMenusByLevel (int? pExpandedUserMenuID)
		{
			return CtrlUser.GetUserMenus (
				new General.Entity.BasicReference () { ID = _Viewer.ApplicationID },
				new General.Entity.BasicReference () { ID = _Viewer.UserGroupID },
				new General.Entity.BasicReference () { ID = pExpandedUserMenuID });
		} 


		public void Save ()
		{
			try
			{
				CtrlBasicReference.RegisterTree<ESF.General.Entity.BasicReference> 
				(
					new ESF.General.Entity.BasicReference () { ID = _Viewer.ApplicationID },
					CtrlBasicReference.Get (new ESF.General.Entity.BasicReference () { ID = _Viewer.UserGroupID }),
					_Viewer.SelectedItems, 
					(int) BasicReferenceTypes.UserMenu
				);

				_Viewer.frmInfoMsg = "Los elementos marcados han sido almacenados";
			}
			catch (Exception pEx)
			{
				LogError (pEx);
			}
		}


		public void ShowMainUserMenus ()
		{
			if (_Viewer.ApplicationID == null)
			{
				_Viewer.lstMainUserMenus = null;
				return;
			}

			try
			{
				_Viewer.lstMainUserMenus = CtrlUser.GetMainUserMenus (
					new General.Entity.BasicReference () { ID = _Viewer.ApplicationID },
					new General.Entity.BasicReference () { ID = _Viewer.UserGroupID }).ToList ();
			}
			catch (Exception pEx)
			{
				LogError (pEx);
			}
		} 

		#endregion
	}
}
